<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Invoices;
use App\Rep\EmployeeRep;
use App\Rep\NumberExtractorRep;
use App\Rep\Rbac\UserRep;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Library\PrintingOptions\PrintingOptions;

class NumberExtractor extends Controller
{
    //抽号机管理
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new NumberExtractorRep();
    }

    /**
     * 管理卡登陆
     * 员工卡号登录
     */
    public function signInCardNumber(Request $request){
        $params = $request->all();
        $userRep = new UserRep();
        $data = $userRep->signInCardNumberRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 员工输入卡号，查询可领取订单
     */
    public function getOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $list = $this->rep->getOrderListByEmployeeCardNumnberRep($params);
        return re(0,$list,'success');
    }

//    public function getDataByDayin(Request $request){
//        $params = $request->all();
//        $data = $this->rep->getDataByDayinRep($params);
//        return re(0,$data,'success');
//    }

    /**
     * 已完成，未完成
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCompletedOrUnfinishedOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $employeeRep = new EmployeeRep();
        $list = $employeeRep->searchCompletedOrUnfinishedOrderChildRep($params);
        return re(0,$list,'success');
    }

    /**
     * 获取订单详情
     * @param Request $request
     */
    public function getOrderChildInfoByEmployeeCardNumnber(Request $request){
        $params = $request->only(['user_id','order_id','order_second_id','order_count','job_type_id']);
        $data = $this->rep->getOrderChildInfoByEmployeeCardNumnberRep($params);
        $order_child = $data['data']['data']->toArray();
        $count = $data['data']['count'];
        $order_child_id_arr = $data['data']['order_child_id_arr'];
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();

        $pritingOptions = new PrintingOptions();
        $array = $pritingOptions->getOrderInvoices($list,$order_child,$count,$order_child_id_arr);
        return re($data['code'],$array,$data['msg']);
    }



    /**
     * 抽号机
     * 员工领取订单
     * @param Request $request
     */
    public function drawOrderChildByEmployeeCardNumnber(Request $request){
        $params = $request->only(['user_id','order_id','order_second_id','order_count','job_type_id']);
        $employeeRep = new EmployeeRep();
        $data = $employeeRep->drawOrderChildRep($params);
        if($data['code'] != 0){
            return re($data['code'],$data['data'],$data['msg']);
        }
        $order_child = $data['data']['data']->toArray();
        $count = $data['data']['count'];
        $order_child_id_arr = $data['data']['order_child_id_arr'];
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();

        $pritingOptions = new PrintingOptions();
        $array = $pritingOptions->getOrderInvoices($list,$order_child,$count,$order_child_id_arr);
        return re($data['code'],$array,$data['msg']);
    }

    /**
     * 查询外包订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getEpibolyOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $list = $this->rep->getEpibolyOrderListByEmployeeCardNumnberRep($params);
        return re(0,$list,'success');
    }

    /**
     * 获取外包详情
     */
    public function getEpibolyOrderInfo(Request $request){
        $params = $request->all();
        $order_child_list = $this->rep->getEpibolyOrderInfoRep($params);
        //p($order_child_list);
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();

        $pritingOptions = new PrintingOptions();
        $array = $pritingOptions->getEpibolyOrderInvoices($list,$order_child_list);
        return re(0,$array,'success');
    }




    /**
     * 生成二维码图片
     * @param $url
     * @param string $img
     * @param string $filename
     */
//    public function getQrcodeImg($url,$file_path, $filename)
//    {
//        if(!file_exists($file_path))
//            mkdir($file_path);
//        //使用QrCode类，生成二维码图片
//        //format() 修改二维码图片格式，目前仅支持PNG、SVG 和 EPS三种格式，必须在其他格式化方法前调用
//        QrCode::format('png')
//            //size() 修改二维码图片尺寸：像素
//            ->size(198)
//            //margin()二维码边距设置,默认为4
//            ->margin(2)
//            //容错级别设置 L（7%）、M（15%）、Q（25%）、H（30%），容错级别越高，字节码回复率越大，二维码里能存储的数据越少
//            //->errorCorrection("L")
//            //merge($filename, $percentage, $absolute)Logo或者头像放到二维码图片中，参数分别为 图片路径，百分比，切换绝对路径
//            //->merge($img, .3, true)
//            //指定编码
//            ->encoding('UTF-8')
//            //generate($text , $filename)$text 二维码数据 $filename 文件名及保存路径
//            ->generate($url, $file_path.$filename);
//    }


    /**
     * 抽号机单据设置
     * 获取抽号机单据设置列表
     */
    public function getInvoicesList(Request $request){
        $list = Invoices::orderBy('sort','asc')->get();
        return re(0,$list,'success');
    }

    /**
     * 抽号机单据设置
     * 设置字段是否显示  field = is_show  value=1
     * 设置字体大小      field = size     value=1
     */
    public function setInvoicesValue(Request $request){
        $id = $request->input('id');
        $field = $request->input('field');
        $value = $request->input('value');
        $model = Invoices::find($id);
        $model->$field = $value;
        $model->save();
        return re(0,$model,'success');
    }

    /**
     * 抽号机单据设置
     * 设置字段顺序
     * @param Request $request
     */
    public function setInvoicesSort(Request $request){
        $arr = $request->input('sort_arr');
        DB::beginTransaction();
        try{
            foreach($arr as $k=>$v){
                Invoices::where('id',$v['id'])->update(['sort'=>$v['sort']]);
            }
            DB::commit();
            re(0,[],'success');
        }catch(\Exception $exception){

            DB::rollBack();
            return re(2001,[],$exception->getMessage());
        }
    }




}
